package com.alibaba.cloud.application.sms.strategy;

import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;

import com.alibaba.cloud.domain.dto.SmsPlaceHolderParameter;
import com.alibaba.cloud.domain.dto.SmsTemplateContext;
import com.alibaba.cloud.exception.CommonException;

import lombok.extern.slf4j.Slf4j;

/**
 * @author cruise
 * @description SmsSendAnyMatchStrategy, 只要占位符参数匹配了短信模板中的任意一个占位符key,就允许发送
 */
@Slf4j
public class SmsSendAnyMatchStrategy implements SmsSendRejectStrategy {

    @Override
    public void reject(SmsTemplateContext templateContext, SmsPlaceHolderParameter parameter) {
        Set<String> parameterKeySet = getParameterSet(parameter);
        if (CollectionUtils.intersection(templateContext.getPlaceHolderKeySet(), parameterKeySet).isEmpty()) {
            log.error("短信占位符替换参数与短信模板完全不匹配,templateContent = {},parameter = {}", templateContext.getTemplateContent(),
                    parameter);
            throw new CommonException(null, "短信占位符替换参数与短信模板完全不匹配");
        }
    }
}
